package nowcoder.offer;

/**
 * 判断是否为平衡二叉树
 *
 * @author briarbear
 * @blog https://briarbear.github.io
 * @date 2018-08-28 10:59
 */
public class IsBalanced {

    /**
     * 尝试二叉树的后序遍历
     */

    private boolean res = true;
    public boolean solution(TreeNode root){
        helper(root,1);

        return res;
    }

    public int helper(TreeNode node,int depth){
        if (node==null)
            return depth-1;
        int left = helper(node.left,depth+1);
        int right = helper(node.right,depth+1);

        if (Math.abs(left-right) > 1)
            res = false;
        return left > right ? left:right;
    }

    public static void main(String[] args) {
        TreeNode root = new TreeNode(1);
        root.left = new TreeNode(2);
        root.left.left = new TreeNode(7);
        IsBalanced is = new IsBalanced();
        is.solution(root);
        System.out.println(is.res);
    }
}
